package com.loong.system.business;

import com.loong.common.mybatis.business.AbstractBusiness;
import com.loong.system.common.backend.param.SystemLetterPageQueryParam;
import com.loong.system.common.forehead.param.DeleteSystemLetterForeheadParam;
import com.loong.system.common.forehead.param.SystemLetterPageQueryForeheadParam;
import com.loong.system.model.SystemLetter;
import com.loong.system.model.SystemLetterTemplate;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Optional;

@Repository
public class SystemLetterBusiness extends AbstractBusiness<SystemLetter> {

    /**
     * 查询未读的站内信
     *
     * @param userId 用户id
     * @return 站内信集合
     */
    public List<SystemLetter> findUnReadLetter(Long userId) {
        Condition condition = new Condition(SystemLetter.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("isRead", false);
        return findByCondition(condition);
    }

    /**
     * 根据关联id查询站内信
     * @param linkId 关联id
     * @return 站内信集合
     */
    public List<SystemLetter> findReadLetterByLinkId(Long linkId){
        Condition condition = new Condition(SystemLetter.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("linkId", linkId);
        criteria.andEqualTo("isRead", true);
        return findByCondition(condition);
    }

    /**
     * 根据参数查询站内信
     * @param param 查询参数
     * @return 站内信集合
     */
    public List<SystemLetter> findLetterByParam(SystemLetterPageQueryForeheadParam param){
        Condition condition = new Condition(SystemLetter.class);
        Example.Criteria criteria = condition.createCriteria();
        if (ObjectUtils.isNotEmpty(param.getUserId())){
            //用户id
            criteria.andEqualTo("userId",param.getUserId());
        }
        if (ObjectUtils.isNotEmpty(param.getIsRead())){
            //是否已读
            criteria.andEqualTo("isRead", param.getIsRead());
        }
        if (ObjectUtils.isNotEmpty(param.getUserType())){
            //用户类型
            criteria.andEqualTo("userType",param.getUserType());
        }
        if (ObjectUtils.isNotEmpty(param.getLetterType())){
            //信件类型
            criteria.andEqualTo("letterType",param.getLetterType());
        }
        if (ObjectUtils.isNotEmpty(param.getTitle())){
            //指定标题为模糊查询的条件
            criteria.andLike("title", "%" + param.getTitle() + "%");
        }
        if (ObjectUtils.isNotEmpty(param.getSendStartTime())) {
            //指定发送的开始时间
            criteria.andGreaterThanOrEqualTo("sendTime", param.getSendStartTime());
        }
        if (ObjectUtils.isNotEmpty(param.getSendEndTime())) {
            //指定发送的结束时间
            criteria.andLessThanOrEqualTo("sendTime", param.getSendEndTime());
        }
        return findByCondition(condition);
    }

    /**
     * 根据参数删除站内信
     * @param userId 用户id
     * @param letterId 站内信id
     */
    public void deleteLettersByParam(Long userId,Long letterId){
        Condition condition = new Condition(SystemLetter.class);
        Example.Criteria criteria = condition.createCriteria();
        criteria.andEqualTo("userId",userId);
        criteria.andEqualTo("id",letterId);
        deleteByCondition(condition);
    }


}
